// 数据库
const pool = require('../pool');
// 游客信息路由
const express = require('express');
const r = express.Router();

// 游客信息添加
// http://127.0.0.1:8888/v1/visinfo/visinfoadd
r.post('/visinfoadd',(req,res,next)=>{
  let obj = req.body;
  console.log(obj);
  pool.query('select * from hqyc_visitorinfo where userid=?',[obj.userid],(err,result)=>{
    if(err){
      next(err)
      return;
    }
    console.log(result);
    result.length!=0&&
    pool.query("insert into hqyc_visitorinfo set ?",[obj],(err,result)=>{
      if(err){
        next(err)
        return;
      }
      console.log(result);
      result.affectedRows==1?res.send({code:1,msg:'游客信息添加成功',data:result}):res.send({code:0,msg:'游客信息添加失败',data:result});
    })
  })
  
})

// 当前用户游客信息查询
// http://127.0.0.1:8888/v1/visinfo/visinfo
r.post('/visinfo',(req,res,next)=>{
  let obj = req.body;
  // console.log(obj);
  pool.query("select * from hqyc_visitorinfo where userid=?",[obj.userid],(err,result)=>{
    if(err){
      next(err)
      return;
    }
    // console.log(result);
    result.length>0?res.send({code:1,msg:'游客信息查询成功',data:result}):res.send({code:0,msg:'游客信息查询失败',data:result});

  })
})

// 已购票游客信息查询
// http://127.0.0.1:8888/v1/visinfo/visinfo
r.post('/shopp',(req,res,next)=>{
  let obj = req.body;
  // console.log(obj);
  pool.query("select v.id,v.vname,v.vnum from hqyc_visitorinfo v where id=?",[obj.id],(err,result)=>{
    if(err){
      next(err)
      return;
    }
    // console.log(result);
    result.length>0?res.send({code:1,msg:'游客信息查询成功',data:result}):res.send({code:0,msg:'游客信息查询失败',data:result});

  })
})

// 游客信息删除
// http://127.0.0.1:8888/v1/visinfo/del
r.post('/del',(req,res,next)=>{
  let obj = req.body;
  // console.log(obj);
  pool.query("delete from hqyc_visitorinfo where id=? and userid=?",[obj.id,obj.userid],(err,result)=>{
    if(err){
      next(err)
      return;
    }
    // console.log(result);
    result.affectedRows==1?res.send({code:1,msg:'游客信息删除成功',data:result}):res.send({code:0,msg:'游客信息添加删除失败',data:result});

  })
})

module.exports = r;